package com.logistic;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.Entity.Product.Description;
import com.Entity.Product.Product;
import com.Entity.Product.ProductImplement;
import com.Entity.Product.Product_tag;
import com.Entity.Product.Product_tagImplement;
import com.Entity.Visitor.Visitor;

public class ProductOwner extends Basic {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title ProductOperator.java
	 * @Package com.logistic
	 * @author ufo
	 */

	private String[] visitorName;
	private String[] password;
	private String[] productId;
	private String[] product_keyName;
	private String[] product_keyValue;
	private String[] productDescription;
	private String[] product_tagName;
	private String[] max;
	private String[] first;

	public String applyTagsToProduct() {
		try {
			ProductImplement product = new Product(getProductId()[0]);
			addSessionToVisitor();
			if (visitor.hasPrivilege(product)) {
				for (int i = 0; i < getProduct_tagName().length; i++) {
					Product_tagImplement tag = Product_tag
							.loadByProduct_tagName(getProduct_tagName()[i]);
					tag.connectWithProduct(product);
				}
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String iteratorProducts() {
		try {
			addSessionToVisitor();
			List list = Product.iteratorProducts(Integer.parseInt(getMax()[0]),
					Integer.parseInt(getFirst()[0]), visitor.getVisitorId());
			Iterator i = list.iterator();
			int j = 0;
			for (; i.hasNext(); j++)
				result.put("productId" + j, i.next().toString());
			if (j > 0) {
				result.put("amountOfProducts",
						Product.countProducts(this.visitor.getVisitorId())
								.toString());
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String altProduct() {
		try {
			addSessionToVisitor();
			ProductImplement productTmp = new Product(getProductId()[0]);
			if (this.visitor.hasPrivilege(productTmp))
				for (int i = 0; i < getProductId().length;)
					productTmp.altValue(getProduct_keyName()[i],
							getProduct_keyValue()[i]);

			result.put("flag", "true");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String clearProduct() {
		try {
			int i = 0;
			addSessionToVisitor();
			for (; i < getProductId().length; i++) {
				ProductImplement productTmp = new Product(getProductId()[i]);
				if (this.visitor.hasPrivilege(productTmp))
					productTmp.altValue("amount", "0");
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String deleteProduct() {
		try {
			int i = 0;
			addSessionToVisitor();
			for (; i < getProductId().length; i++) {
				ProductImplement productTmp = new Product(getProductId()[i]);
				if (this.visitor.hasPrivilege(productTmp))
					productTmp.delete();
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String addProduct() {
		try {
			if (isOnline() && null != getProductDescription()
					&& null != getProduct_keyName()
					&& null != getProduct_keyValue()) {
				addSessionToVisitor();
				ProductImplement product = new Product(new Description(
						getProductDescription()[0]));
				for (int i = 0; i < getProduct_keyName().length; i++)
					product.addKey(getProduct_keyName()[i],
							getProduct_keyValue()[i]);
				product.addKey(PRODUCT_KEYNAME_OWNER, visitor.getVisitorId());
				product.save();
				result.put("productId", product.getProductId());
				result.put("flag", "true");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("flag", "false");
		}
		return SUCCESS;
	}

	public String[] getVisitorName() {
		return visitorName;
	}

	public void setVisitorName(String[] visitorName) {
		this.visitorName = visitorName;
	}

	public String[] getPassword() {
		return password;
	}

	public void setPassword(String[] password) {
		this.password = password;
	}

	public String[] getProductId() {
		return productId;
	}

	public void setProductId(String[] productId) {
		this.productId = productId;
	}

	public String[] getProduct_keyName() {
		return product_keyName;
	}

	public void setProduct_keyName(String[] product_keyName) {
		this.product_keyName = product_keyName;
	}

	public String[] getProduct_keyValue() {
		return product_keyValue;
	}

	public void setProduct_keyValue(String[] product_keyValue) {
		this.product_keyValue = product_keyValue;
	}

	public String[] getProductDescription() {
		return productDescription;
	}

	public void setProductDescription(String[] productDescription) {
		this.productDescription = productDescription;
	}

	public void setVisitor(String visitorName, String password) {
		this.visitor = new Visitor(visitorName, password);
	}

	@Override
	public Map<String, String> getResult() {
		// TODO 自动生成的方法存根
		return this.result;
	}

	public String[] getMax() {
		return max;
	}

	public void setMax(String[] max) {
		this.max = max;
	}

	public String[] getFirst() {
		return first;
	}

	public void setFirst(String[] first) {
		this.first = first;
	}

	public String[] getProduct_tagName() {
		return product_tagName;
	}

	public void setProduct_tagName(String[] product_tagName) {
		this.product_tagName = product_tagName;
	}
}
